# -*- coding:utf-8 -*-

from selenium.webdriver.common.by import By

from PageClass.fscIndexPageClass.fscIndexPage import FscIndexPage
from Util import logger


class AuditAdjustDirectorPage(FscIndexPage):
    _selectFirstGroup = (By.XPATH, '/html/body/div[5]/div[1]/div[1]/ul/li')
    _selectFirstGroup1 = (By.XPATH, '/html/body/div[4]/div[1]/div[1]/ul/li')

    _selectFirstOperatorUser = (By.XPATH, '/html/body/div[5]/div[1]/div[1]/ul/li')
    _selectFirstOperatorUser1 = (By.XPATH, '/html/body/div[4]/div[1]/div[1]/ul/li')

    def __init__(self, driver):
        super().__init__(driver)

    # 输入单号
    def input_selectBoeNum(self, boeNo):
        super().input_selectBoeNum(boeNo)

    # 点击查询
    def click_selectButton(self):
        super().click_selectButton()

    # 点击查询结果第一条
    def click_selectResult(self):
        super().click_selectResult()

    def getSelectResult(self):
        return self._selectResult

    # 分配到组
    _distributeToGroup = (By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/button[2]/span')
    _selectGroup = (By.ID, 'form_fGroupId')
    _groupName = (By.CSS_SELECTOR, 'body>div.el-select-dropdown.el-popper>div.el-scrollbar>'
                                   'div.el-select-dropdown__wrap.el-scrollbar__wrap>ul>li')
    _selectGroupSubmit = (By.XPATH, '//*[@id="form"]/div[3]/div/button[2]')

    def selectGroupName(self, groupName):
        elementList = self.find_elements(*self._groupName)
        for element in elementList:
            if element.text == groupName:
                self._clickElement(element)
                break

    def choiceGroup(self, groupType, boeNo, num=1):
        if num > 3:
            logger.error(f'共享运营，单号：{boeNo}，{num - 1} 次分组均失败，请确认。')
            raise AssertionError(f"单号：{boeNo} 分组失败，组名：{groupType}")
        try:
            self.click(*self._distributeToGroup)
            logger.info("点击分配到组")
            self.click(*self._selectGroup)
            self.selectGroupName(groupType)
            logger.info(f"选择组：{groupType}")
            self.click(*self._selectGroupSubmit)
            logger.info("点击提交按钮")
        except Exception as e:
            logger.warning(f'共享运营，单号：{boeNo}，第 {num} 次分组失败，错误原因：{str(e)}')
            return self.choiceGroup(groupType, boeNo, num + 1)

    # 分配人员
    _distributeToStaff = (By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/button[3]/span')
    _selectOperatorUser = (By.ID, 'form_operatorUserId')
    _selectPerson = (By.CSS_SELECTOR, 'body>div.el-select-dropdown.el-popper>div.el-scrollbar>'
                                      'div.el-select-dropdown__wrap.el-scrollbar__wrap>ul')
    _selectOperatorUserSubmit = (By.XPATH, '//*[@id="form"]/div[4]/div/button[2]')

    def selectPersonName(self, userName):
        self.selectGroupName(userName)

    def choiceOperatorUser(self, userType, boeNo, num=1):
        if num > 3:
            logger.error(f'共享运营，单号：{boeNo}，{num - 1} 次分人均失败，请确认。')
            raise AssertionError(f"单号：{boeNo} 分人失败，人名：{userType}")
        try:
            self.click(*self._distributeToStaff)
            logger.info("点击分配人员")
            self.click(*self._selectOperatorUser)
            self.selectPersonName(userType)
            logger.info(f"选择人：{userType}")
            self.click(*self._selectOperatorUserSubmit)
            logger.info("点击提交按钮")
        except Exception as e:
            logger.warning(f'共享运营，单号：{boeNo}，第 {num} 次分人失败，错误原因：{str(e)}')
            return self.choiceOperatorUser(userType, boeNo, num + 1)

    # 任务收回
    _taskTakeToBack = (By.XPATH, '//*[@id="app"]/section/section/main/div/div/div/div[2]/button[4]/span')

    def click_taskTakeToBack(self):
        self.click(*self._taskTakeToBack)
        logger.info("点击任务收回")

    # 查看所属组信息
    _fGroupName = (By.XPATH, '//td[contains(@class, "fGroupName")]')

    def fGroupNameIsEmpty(self):
        if not self.get_elementText(*self._fGroupName):
            return True
